package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.RegisteredPageBean;
import com.example.administrator.liveapp.bean.RegisteredYzPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.CheckInfoUtill;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 手机号注册activity(已经没有用了)
 */

public class RegisteredPage extends Activity {
    private TextView login_title;
    private Button registered_button;
    private TextView registered_title;
    private RadioButton login_return;
    private EditText registered_phone;
    private EditText registered_validation;
    private Button button_code;
    private EditText registered_password;
    private SharedPrefsUtil sp;//缓存工具类
    private String password;//获取当前密码
    private String InputCode;
    private RegisteredYzPageBean CodeBean;//获取注册验证码试实体类
    private RegisteredPageBean RegisteredBean;//注册实体类
    private int value;//验证码
    private String phones;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registered_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
        listener();
    }

    private void setData() {
        sp = new SharedPrefsUtil();
        login_title.setVisibility(View.GONE);
        registered_title.setVisibility(View.VISIBLE);
    }

    private void initView() {
        login_title = (TextView) findViewById(R.id.login_title);
        registered_button = (Button) findViewById(R.id.registered_button);
        registered_title = (TextView) findViewById(R.id.registered_title);
        login_return = (RadioButton) findViewById(R.id.login_return);
        registered_phone = (EditText) findViewById(R.id.registered_phone);
        registered_validation = (EditText) findViewById(R.id.registered_validation);
        button_code = (Button) findViewById(R.id.button_code);
        registered_password = (EditText) findViewById(R.id.registered_password);
    }

    /**
     * 倒计时:
     */
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            button_code.setEnabled(false);
            button_code.setBackgroundColor(ContextCompat.getColor(RegisteredPage.this,R.color.code));
            button_code.setText((millisUntilFinished / 1000) + "秒后可重发");
        }
        @Override
        public void onFinish() {
            button_code.setEnabled(true);
            button_code.setBackgroundColor(ContextCompat.getColor(RegisteredPage.this,R.color.code2));
            button_code.setText("获取验证码");
        }
    };

    private void listener() {
        button_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                phones = registered_phone.getText().toString();
                String codeUrl = AppUrl.URL + AppUrl.RegistrationCodeURL;
                Map<String,String> map=new HashMap<>();
                map.put("User_Phone", phones);
                map.put("Type", "1");
                XUtils.Get(codeUrl,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            if(code.equals("4400")){
                                Gson gson = new Gson();
                                CodeBean = gson.fromJson(result, RegisteredYzPageBean.class);
                                value = CodeBean.getData().getValue();
                                Toast.makeText(RegisteredPage.this,"已发送验证码",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(RegisteredPage.this,"发送验证码失败",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        LogUtil.e(ex.getMessage()+"联网请求失败");
                    }
                });
            }
        });
        registered_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phones = registered_phone.getText().toString();
                password = registered_password.getText().toString();
                InputCode =registered_validation.getText().toString();
                String url = AppUrl.URL + AppUrl.RegistrationSubmittedURL;
                if(CheckInfoUtill.isMobileNO(phones)){
                    if(CheckInfoUtill.isPasswordNO(password)){
                        Map<String,String> map=new HashMap<>();
                        map.put("phone", phones);
                        map.put("identifying", InputCode);
                        map.put("password", password);
                        if(InputCode.equals(""+value)){
                            XUtils.Get(url,map,new MyCallBack<String>(){
                                @Override
                                public void onSuccess(String result) {
                                    super.onSuccess(result);
                                    JSONObject jsonObject = null;
                                    try {
                                        jsonObject = new JSONObject(result);
                                        String code = jsonObject.optString("code");
                                        if(code.equals("4100")){
                                            Gson gson = new Gson();
                                            RegisteredBean = gson.fromJson(result,RegisteredPageBean.class);
                                            Intent intent = new Intent();
                                            intent.putExtra("phone2",phones);
                                            intent.putExtra("password",password);
                                            intent.putExtra("consumerId",RegisteredBean.getConsumerid());
                                            intent.setClass(RegisteredPage.this,PersonalInformationPage.class);
                                            startActivity(intent);
                                            overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                                            Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                                        }else{
                                            Toast.makeText(getApplicationContext(),"手机号已被注册",Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {
                                    super.onError(ex, isOnCallback);
                                    LogUtil.e(ex.getMessage()+"联网请求失败");
                                }
                            });
                        }else{
                            Toast.makeText(getApplicationContext(),InputCode+"验证码输入错误"+value,Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(),"密码最少6位数,最大16位数",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                }
            }
        });

        login_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
